package org.jadapter.tests.spring;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.jadapter.registry.AdapterRegistry;
import org.jadapter.tests.examples.CommaSeparated;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestContainer extends TestCase {

	ApplicationContext ctx;
	

	@Override
	public void setUp() {
		ctx = new ClassPathXmlApplicationContext
			("org/jadapter/tests/spring/context.xml");
	}
	
	
	public void testShouldBeEasyToCreateAdapterRegistryInSpring(){
	    AdapterRegistry adapters = (AdapterRegistry)ctx.getBean("adapters");
	    List<String> list = Arrays.asList("Foo", "Bar");
        
        assertEquals("Foo,Bar", adapters.transform(list, CommaSeparated.class)
                .getCommaSeparated());    	
        
        Map sequence = adapters.transform(list, Map.class);
        assertNotNull(sequence);
	    assertEquals(sequence.get(1), "Bar");
	    
	}


	public void testShouldCreateRegistryBean() {
		ctx.getBean("adapters");		
		
		
	}						
	
	public void testMultipleAdaptations() {
        AdapterRegistry adapters = (AdapterRegistry)ctx.getBean("adapters");
        List<String> list = Arrays.asList("Foo", "Bar");
        Map sequence = adapters.transform(list, Map.class);
        CommaSeparated commas = adapters.transform(sequence, CommaSeparated.class);
        assertEquals("0:Foo,1:Bar", commas.getCommaSeparated());
	    
	}
	
	public void testImplicitAdaptation() {
	    // List -> Map -> CommaSeparated
        AdapterRegistry adapters = (AdapterRegistry)ctx.getBean("adaptersNoList");
        
        List<String> list = Arrays.asList("Foo", "Bar");

        Map map = adapters.transform(list, Map.class);
        
        
        CommaSeparated commas = adapters.transform(map, CommaSeparated.class);
        assertEquals("0:Foo,1:Bar", commas.getCommaSeparated());         
	    
	}
	
    @Test public void testShouldTransformUsingTransitivity() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext
            ("org/jadapter/tests/spring/context.xml");
        
        AdapterRegistry adapters = (AdapterRegistry)ctx.getBean("adaptersNoList");

        List<String> list;  //Arrays.asList("Foo", "Bar");

        list = new LinkedList();
        list.add("Foo"); list.add("Bar");
        
        // List -> Map -> CommaSeparated
        CommaSeparated commas = adapters.transform(list, CommaSeparated.class);
        assertEquals("0:Foo,1:Bar", commas.getCommaSeparated());       
    }

	
	
}
